package org.hit.burkun.model;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FenciContent {
	
	public FenciContent(String mid, String termStr, String atSomeOne, String topics, String otherTopics){
		this.mid = mid;
		this.otherTopics = getOthers(otherTopics);
		this.topics = getOthers(topics);
		this.terms = getFenciTerms(termStr);
		this.atSomeone = getOthers(atSomeOne);
	}
	
	private static Collection<String> getFenciTerms(String ts){
		if(ts !=null && !ts.isEmpty()){
			String[] terms = ts.split(",");
			Collection<String> res = new LinkedList<>();
			for(String term : terms){
				if(isChinese(term) || isEnglishOrDigit(term)){
					res.add(term);
				}
			}
			return res;
		}else{
			return null;
		}
	}
	
	private static Set<String> getOthers(String ts){
		if(ts == null || ts.isEmpty()) return null;
		String[] somes = ts.split("|$|");
		Set<String> set = new HashSet<>();
		for(String s : somes){
			set.add(s);
		}
		return set;
	}
	
	
	 public static boolean isChinese(String str) {
		  String regEx1 = "[\\u4e00-\\u9fa5]+";
		  String regEx2 = "[\\uFF00-\\uFFEF]+";
		  String regEx3 = "[\\u2E80-\\u2EFF]+";
		  String regEx4 = "[\\u3000-\\u303F]+";
		  String regEx5 = "[\\u31C0-\\u31EF]+";
		  Pattern p1 = Pattern.compile(regEx1);
		  Pattern p2 = Pattern.compile(regEx2);
		  Pattern p3 = Pattern.compile(regEx3);
		  Pattern p4 = Pattern.compile(regEx4);
		  Pattern p5 = Pattern.compile(regEx5);
		  Matcher m1 = p1.matcher(str);
		  Matcher m2 = p2.matcher(str);
		  Matcher m3 = p3.matcher(str);
		  Matcher m4 = p4.matcher(str);
		  Matcher m5 = p5.matcher(str);
		  return  (m1.find() || m2.find() || m3.find() || m4.find() || m5.find());
	}
	 
	 public static boolean isEnglishOrDigit(String str){
		 String regEx1 = "[\\w\\d]+";
		 Pattern p1 = Pattern.compile(regEx1);
		 Matcher m1 = p1.matcher(str);
		 return m1.find();
	 }

	
	public String mid;
	public Collection<String> terms;
	public Set<String> topics;
	public Set<String> otherTopics;
	public Set<String> atSomeone;
	
}
